﻿/* Layout 
------------------------------------------------------------*/
/* Menu */
function closeMenuItems() {
    $("[id^=popup]").removeClass("clicked");
    $("[id^=popup]").fadeOut();
    $("[id^=popup]").addClass("unclicked");
}

function ShowMenuPopup(elementId) {
    var panel = document.getElementById(elementId);
    var className = panel.className;

    $("[id^=popup]").fadeOut();
    $("[id^=popup]").removeClass("clicked");

    if (className == 'unclicked') {

        $(panel).addClass("clicked");
        $(panel).fadeIn();
        if (elementId == 'popupSearch') {
            $("#SearchName").focus();
        }
        $("[id^=popup]").corner();
    }
    else {
        $("[id^=popup]").addClass("unclicked");
        $("[id^=popup]").fadeOut();
    }
}
/* Loader */
$(document).ready(function BindLoader() {
    $("#Loader").ajaxStart(function() {
        $(this).show();
    });
    $("#Loader").ajaxStop(function() {
        $(this).hide();
    });
});
/* AdminPanel 
------------------------------------------------------------*/
function ShowHideAdminPanel() {
    var panel = document.getElementById('admin');
    if (panel.className == 'visible') {
        $("#admin").removeClass();
        $("#admin").addClass("hidden");
    }
    else {
        $("#admin").removeClass();
        $("#admin").addClass("visible");
    }
}
/* IMPORT 
------------------------------------------------------------*/
function UpdateImportList() {

    function loadPriceProductContentPart() {
        $.ajax({ url: "/Import/LoadPriceProductPart",
            type: 'POST',
            data: {},
            success: function (responce) {
                $("#productsTable").append(responce);
            },
            error: function (xhr) {
                alert("Ошибка отправки запроса!");
            }
        });
    };

    $(window).scroll(function () {
        /* Load AnotherContent() function when scroll down to the bottom of page. */
        if (getScrollTop() > $(document).height() - $(window).height() - 100) {
            loadPriceProductContentPart();
        }
    });
}
/* PRODUCTS 
------------------------------------------------------------*/
function UpdateProductsGalery() {

    function loadContentGaleryPart() {
        $.ajax({ url: "/Products/LoadContentGaleryPart",
            type: 'POST',
            data: {},
            success: function (responce) {
                $("#productsGalery").append(responce);
            },
            error: function (xhr) {
                alert("Ошибка отправки запроса!");
            }
        });
    };

    $(window).scroll(function () {
        /* Load AnotherContent() function when scroll down to the bottom of page. */
        if (getScrollTop() > $(document).height() - $(window).height() - 100) {
            loadContentGaleryPart();
        }
    });
}

function UpdateProductsList() {

    function loadContentListPart() 
    {
        var value = $("#hCategoryId").val();
        if (value != null) {
            $.ajax({ url: "/Products/LoadContentListPart",
                type: 'POST',
                data: { categoryId: value },
                success: function (responce) {
                    $("#productsTable").append(responce);
                },
                error: function (xhr) {
                    alert("Ошибка отправки запроса!");
                }
            });            
        }

    };

    $(window).scroll(function () 
    {
        /* Load AnotherContent() function when scroll down to the bottom of page. */
        if (getScrollTop() > $(document).height() - $(window).height() - 100) 
        {
            loadContentListPart();
        }
    });
}

function getScrollTop() {
    if (typeof pageYOffset != 'undefined') {
        //most browsers
        return pageYOffset;
    }
    else {
        var B = document.body; //IE 'quirks'
        var D = document.documentElement; //IE with doctype
        D = (D.clientHeight) ? D : B;
        return D.scrollTop;
    }
}

/* Orders 
------------------------------------------------------------*/
function GetCustomerDetailsByKey() {
    $.ajax({ url: "/Orders/GetCustomerDetailsByKey",
        type: 'POST',
        data: { customerKey: $("input#tbCustomerKey").val() },
        success: function (responce) {
            $("#CustomerDetails").replaceWith(responce);
        },
        error: function (xhr) {
            alert("Ошибка!");
        }
    });
}

function UpdatePrice() {
    var nums = $('input[id^="num"]');
    var values = "";
    for (var i = 0; i < nums.size(); i++) {
        values += nums[i].value + ",";
    }
    $('input[id^="num"]').attr("disabled", true);
    $.ajax({ url: "/Orders/RecaluclateSumPrice",
        type: 'POST',
        data: { itemNums: values },
        success: function (responce) {
            $("#SumPrice").replaceWith(responce);
            $('input[id^="num"]').attr("disabled", false);
        },
        error: function (xhr) {
            alert("Ошибка!");
            $('input[id^="num"]').attr("disabled", false);
        }
    });
}

function ChangeUserType(clear) 
{
    var selectedValue = $("input[name='gUser']:checked").val();
    if (selectedValue == 'New') 
    {
        $("#btnCustomerKey").attr("disabled", "true");
        $("#tbCustomerKey").attr("disabled", "true");
       
        if (clear) {
            $("input#Name").val("");
            $("input#Phone").val("");
            $("input#Email").val("");
            $("input#DeliveryAddress").val("");
        }
        
        $("input#Name").removeAttr("disabled");        
        $("input#Phone").removeAttr("disabled");
        $("input#Email").removeAttr("disabled");
        $("input#DeliveryAddress").removeAttr("disabled");
    }
    else 
    {
        $("#btnCustomerKey").removeAttr("disabled");
        $("#tbCustomerKey").removeAttr("disabled");

        $("input#Name").attr("disabled", "true");
        $("input#Phone").attr("disabled", "true");
        $("input#Email").attr("disabled", "true");
        $("input#DeliveryAddress").attr("disabled", "true");        
    }
}


/* MISC
-------------------------------------------------------------------*/
function Pause(millisecondi) 
{
    var now = new Date();
    var exitTime = now.getTime() + millisecondi;

    while (true) {
        now = new Date();
        if (now.getTime() > exitTime) return;
    }
}